﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _4_8
{
    public partial class MainForm : Form
    {
        private MenuStrip menuStrip1;
        private ToolStripMenuItem menuProgram;
        private ToolStripMenuItem menuHelp;
        private ToolStripMenuItem menuConfig;
        private ToolStripMenuItem menuCourse;
        private ToolStripMenuItem menuTrigonometric;
        private ToolStripMenuItem menuButterfly;
        private ToolStripMenuItem menuExit;
        private ToolStripMenuItem menuAbout;
        private ToolStrip toolStrip1;
        private ToolStripButton toolStripButton1;
        private ToolStripButton toolStripButton2;
        private ToolStripButton toolStripButton3;
        private ToolStripButton toolStripButton4;
        private Label label1;

        private Form2 formCourse;
        private Form3 formTrigonometric;
        private Form4 formButterfly;

        public MainForm()
        {
            InitializeComponent();
            label1.Text = "Visual C#\n多重窗体应用程序演示\n版权所有 侵权必究";
        }

        private void InitializeComponent()
        {
            this.menuStrip1 = new MenuStrip();
            this.menuProgram = new ToolStripMenuItem();
            this.menuConfig = new ToolStripMenuItem();
            this.menuCourse = new ToolStripMenuItem();
            this.menuTrigonometric = new ToolStripMenuItem();
            this.menuButterfly = new ToolStripMenuItem();
            this.menuExit = new ToolStripMenuItem();
            this.menuHelp = new ToolStripMenuItem();
            this.menuAbout = new ToolStripMenuItem();
            this.toolStrip1 = new ToolStrip();
            this.toolStripButton1 = new ToolStripButton();
            this.toolStripButton2 = new ToolStripButton();
            this.toolStripButton3 = new ToolStripButton();
            this.toolStripButton4 = new ToolStripButton();
            this.label1 = new Label();
            this.SuspendLayout();

            this.menuProgram.Text = "程序(P)";
            this.menuConfig.Text = "计算机配置(1)...";
            this.menuCourse.Text = "选课程序(2)...";
            this.menuCourse.Click += MenuCourse_Click;
            this.menuTrigonometric.Text = "三角函数运算(3)...";
            this.menuTrigonometric.Click += MenuTrigonometric_Click;
            this.menuButterfly.Text = "蝴蝶飞(4)...";
            this.menuButterfly.Click += MenuButterfly_Click;
            this.menuExit.Text = "退出(X)";
            this.menuExit.Click += MenuExit_Click;
            this.menuProgram.DropDownItems.AddRange(new ToolStripItem[] { this.menuConfig, this.menuCourse, this.menuTrigonometric, this.menuButterfly, this.menuExit });

            this.menuAbout.Text = "关于(A)...";
            this.menuAbout.Click += MenuAbout_Click;
            this.menuHelp.Text = "帮助(H)";
            this.menuHelp.DropDownItems.AddRange(new ToolStripItem[] { this.menuAbout });

            this.menuStrip1.Items.AddRange(new ToolStripItem[] { this.menuProgram, this.menuHelp });
            this.Controls.Add(this.menuStrip1);

            this.toolStripButton1.Text = "程序1";
            this.toolStripButton2.Text = "程序2";
            this.toolStripButton2.Click += ToolStripButton2_Click;
            this.toolStripButton3.Text = "程序3";
            this.toolStripButton3.Click += ToolStripButton3_Click;
            this.toolStripButton4.Text = "程序4";
            this.toolStripButton4.Click += ToolStripButton4_Click;
            this.toolStrip1.Items.AddRange(new ToolStripItem[] { this.toolStripButton1, this.toolStripButton2, this.toolStripButton3, this.toolStripButton4 });
            this.toolStrip1.Dock = DockStyle.Top;
            this.Controls.Add(this.toolStrip1);

            this.label1.Dock = DockStyle.Fill;
            this.label1.TextAlign = ContentAlignment.MiddleCenter;
            this.Controls.Add(this.label1);

            this.Text = "程序演示";
            this.Size = new Size(300, 200);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void MenuCourse_Click(object sender, EventArgs e)
        {
            if (formCourse == null || formCourse.IsDisposed)
            {
                formCourse = new Form2();
                formCourse.Text = "选课程序";
                formCourse.Show();
            }
            else
            {
                formCourse.Activate();
            }
        }

        private void MenuTrigonometric_Click(object sender, EventArgs e)
        {
            if (formTrigonometric == null || formTrigonometric.IsDisposed)
            {
                formTrigonometric = new Form3();
                formTrigonometric.Text = "三角函数运算";
                formTrigonometric.Show();
            }
            else
            {
                formTrigonometric.Activate();
            }
        }

        private void MenuButterfly_Click(object sender, EventArgs e)
        {
            if (formButterfly == null || formButterfly.IsDisposed)
            {
                formButterfly = new Form4();
                formButterfly.Text = "蝴蝶飞";
                formButterfly.Show();
            }
            else
            {
                formButterfly.Activate();
            }
        }

        private void MenuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MenuAbout_Click(object sender, EventArgs e)
        {
            Form aboutForm = new Form();
            aboutForm.Text = "关于本程序";
            aboutForm.Size = new Size(300, 200);
            Label lblInfo = new Label();
            lblInfo.Dock = DockStyle.Fill;
            lblInfo.TextAlign = ContentAlignment.MiddleCenter;
            lblInfo.Text = "本程序通过设计菜单和工具栏\n整合若干本章的习题程序，帮助读者\n掌握Windows多重窗体应用程序的设计方法。";
            Button btnClose = new Button();
            btnClose.Text = "关闭";
            btnClose.Dock = DockStyle.Bottom;
            btnClose.Click += (s, args) => aboutForm.Close();
            aboutForm.Controls.Add(lblInfo);
            aboutForm.Controls.Add(btnClose);
            aboutForm.ShowDialog();
        }

        private void ToolStripButton2_Click(object sender, EventArgs e)
        {
            MenuCourse_Click(sender, e);
        }

        private void ToolStripButton3_Click(object sender, EventArgs e)
        {
            MenuTrigonometric_Click(sender, e);
        }

        private void ToolStripButton4_Click(object sender, EventArgs e)
        {
            MenuButterfly_Click(sender, e);
        }
    }

    public class Form2 : Form
    {
        private ListBox listBox1;
        private ListBox listBox2;

        public Form2()
        {
            InitializeComponent();
            listBox1.Items.AddRange(new string[] { "C#程序设计", "数据库技术与应用", "多媒体技术与应用" });
            listBox1.Sorted = true;
            listBox2.Sorted = true;
        }

        private void InitializeComponent()
        {
            this.listBox1 = new ListBox();
            this.listBox2 = new ListBox();
            this.SuspendLayout();
            this.listBox1.Location = new Point(20, 20);
            this.listBox1.Size = new Size(120, 150);
            this.listBox2.Location = new Point(160, 20);
            this.listBox2.Size = new Size(120, 150);
            this.Controls.Add(listBox1);
            this.Controls.Add(listBox2);
            this.Text = "选课程序";
            this.Size = new Size(300, 200);
            this.ResumeLayout(false);
        }
    }

    public class Form3 : Form
    {
        private TextBox textBox1;
        private TextBox textBox2;
        private RadioButton radioButton1;
        private RadioButton radioButton2;
        private RadioButton radioButton3;
        private CheckBox checkBox1;
        private CheckBox checkBox2;
        private CheckBox checkBox3;

        public Form3()
        {
            InitializeComponent();
            textBox1.Text = "3.14159";
        }

        private void InitializeComponent()
        {
            this.textBox1 = new TextBox();
            this.textBox2 = new TextBox();
            this.radioButton1 = new RadioButton();
            this.radioButton2 = new RadioButton();
            this.radioButton3 = new RadioButton();
            this.checkBox1 = new CheckBox();
            this.checkBox2 = new CheckBox();
            this.checkBox3 = new CheckBox();
            this.SuspendLayout();
            this.textBox1.Location = new Point(80, 20);
            this.textBox2.Location = new Point(80, 60);
            this.radioButton1.Text = "Sin";
            this.radioButton1.Location = new Point(20, 100);
            this.radioButton2.Text = "Cos";
            this.radioButton2.Location = new Point(80, 100);
            this.radioButton3.Text = "Tan";
            this.radioButton3.Location = new Point(140, 100);
            this.checkBox1.Text = "粗体";
            this.checkBox1.Location = new Point(20, 140);
            this.checkBox2.Text = "斜体";
            this.checkBox2.Location = new Point(80, 140);
            this.checkBox3.Text = "红色";
            this.checkBox3.Location = new Point(140, 140);
            this.Controls.Add(textBox1);
            this.Controls.Add(textBox2);
            this.Controls.Add(radioButton1);
            this.Controls.Add(radioButton2);
            this.Controls.Add(radioButton3);
            this.Controls.Add(checkBox1);
            this.Controls.Add(checkBox2);
            this.Controls.Add(checkBox3);
            this.Text = "三角函数运算";
            this.Size = new Size(300, 200);
            this.ResumeLayout(false);
        }
    }

    public class Form4 : Form
    {
        private PictureBox pictureBox1;
        private PictureBox pictureBox2;
        private Timer timer1;
        private Button button1;
        private Button button2;
        private int i = 0;

        public Form4()
        {
            InitializeComponent();
            pictureBox1.Image = Image.FromFile("bfly2.png");
            timer1.Interval = 200;
            timer1.Enabled = false;
        }

        private void InitializeComponent()
        {
            this.pictureBox1 = new PictureBox();
            this.pictureBox2 = new PictureBox();
            this.timer1 = new Timer();
            this.button1 = new Button();
            this.button2 = new Button();
            this.SuspendLayout();
            this.pictureBox1.Location = new Point(100, 80);
            this.pictureBox1.Size = new Size(150, 120);
            this.pictureBox2.Location = new Point(100, 80);
            this.pictureBox2.Size = new Size(150, 120);
            this.pictureBox2.Image = Image.FromFile("bfly1.png");
            this.pictureBox2.Visible = false;
            this.button1.Text = "开始";
            this.button1.Location = new Point(100, 220);
            this.button1.Click += Button1_Click;
            this.button2.Text = "停止";
            this.button2.Location = new Point(180, 220);
            this.button2.Click += Button2_Click;
            this.Controls.Add(pictureBox1);
            this.Controls.Add(pictureBox2);
            this.Controls.Add(button1);
            this.Controls.Add(button2);
            this.Text = "蝴蝶飞";
            this.Size = new Size(400, 300);
            this.ResumeLayout(false);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            pictureBox1.Visible = true;
            pictureBox2.Visible = false;
            i = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (i % 2 == 0)
            {
                pictureBox1.Visible = false;
                pictureBox2.Visible = true;
            }
            else
            {
                pictureBox1.Visible = true;
                pictureBox2.Visible = false;
            }
            i++;
        }
    }
}
